package org.flexthinker.binding.value
{
    /**
     * A class that can be used to trigger an event on a group of objects. Mainly
     * intended to be used to trigger flush/revert in
     * <code>BufferedValueModel</code> but is useful in general.
     */
    public class CommitTrigger
    {

        private var triggerListeners:Vector.<CommitTriggerListener> = new Vector.<CommitTriggerListener>();
        /**
         * Constructs a <code>CommitTrigger</code>.
         */
        public function CommitTrigger()
        {
        }

        public function commit():void
        {
            for each ( var listener:CommitTriggerListener in triggerListeners )
            {
                listener.commit();
            }
        }

        public function revert():void
        {
            for each ( var listener:CommitTriggerListener in triggerListeners )
            {
                listener.revert();
            }
        }

        /**
         * Adds the provided listener to the list of listeners that will be notified
         * whenever a commit or revert event is fired.
         *
         * @param listener the <code>CommitTriggerListener</code> to add
         */
        public function addCommitTriggerListener(listener:CommitTriggerListener):void
        {
            triggerListeners.push(listener);
        }

        /**
         * Removed the provided listener to the list of listeners that will be
         * notified whenever a commit or revert event is fired.
         *
         * @param listener the <code>CommitTriggerListener</code> to remove
         */
        public function removeCommitTriggerListener(listener:CommitTriggerListener):void
        {
            var idx:int = triggerListeners.indexOf(listener);
            if ( idx >= 0 )
            {
                triggerListeners.splice(idx, 1);
            }
        }
    }
}